package user;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;


import jade.core.Agent;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.sparql.vocabulary.FOAF;
import com.hp.hpl.jena.util.FileManager;

import feedback.FeedbackManager;
import feedback.RATING;


public class UserAgent extends Agent{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private FeedbackManager fdManager;
	private SemWebSearch search;
	private String profileLoc;
	
	private Model model;
	private String resourceURI;
	private ArrayList<String> interests;
	private ArrayList<SearchResult> results;

	protected void setup() {
		System.out.println("UserAgent (" + getAID().getName()
				+ ") says hello.");

		Object[] args = getArguments();
		if (args != null && args.length > 0) {
			this.profileLoc = (String) args[0];
			System.out.println("[Profile Loc]: " + this.profileLoc);
		} else {
			System.out.println("Input error, try again");
			doDelete();
		}
		
		System.out.println("Agent is being initialized...\n");

		this.init(this.profileLoc);
		//Behaviour behaviour = new SearchingBehaviour(noun, attribute);
		//addBehaviour(behaviour);
		//behaviour.action();
	}
	
	private void init(String inputFile) {
		
		// create an empty model
		this.model = ModelFactory.createDefaultModel();
		this.model.setNsPrefix("foaf", FOAF.NS);
		
		// use the FileManager to find the input file
		InputStream in = FileManager.get().open(inputFile);
		if (in == null) {
			throw new IllegalArgumentException("File: " + inputFile
					+ " not found");
		}

		// read the RDF/XML file
		this.model.read(in, null);

		// write it to standard out
		System.out.println("User's profile in FOAF: ------------------------------");
		this.model.write(System.out);
		
		Iterator<Resource> it = this.model.listResourcesWithProperty(FOAF.interest);
		if(it.hasNext()){
			this.resourceURI =  it.next().getURI();
			Resource profile = this.model.getResource(this.resourceURI);
			String interestsStr = profile.getProperty(FOAF.interest).getString();
			StringTokenizer tokenizer = new StringTokenizer(interestsStr, ";");
			this.interests = new ArrayList<String>();
			while(tokenizer.hasMoreTokens()) {
				this.interests.add(tokenizer.nextToken());
			}
			// execute search task
			this.search = new SemWebSearch(this.model, this.resourceURI, this.interests);
			this.results = this.search.advancedSearch();
		}
		else
			System.out.println("[Error] : No interst in current user profile.");
		
		// temporally generate random rating for test
		this.randomlyRating();
//		this.printAll();
		
		// manage feedback
		this.fdManager = new FeedbackManager();
		this.fdManager.saveDB(this.results);
	}

	// temporally generate random rating for test
	private void randomlyRating() {
		for(int index = 0; index < this.results.size(); index ++) {
			double random = Math.random();
			if(0.8 < random && random <= 1)
				this.results.get(index).setRating(RATING.EXCELLENT);
			else if(0.6 < random && random <= 0.8)
				this.results.get(index).setRating(RATING.GOOD);
			else if(0.4 < random && random <= 0.6)
				this.results.get(index).setRating(RATING.AVERAGE);
			else if(0.2 < random && random <= 0.4)
				this.results.get(index).setRating(RATING.FAIR);
			else if(0 < random && random <= 0.2)
				this.results.get(index).setRating(RATING.POOR);
			else if(0 == random)
				this.results.get(index).setRating(RATING.NORATING);
			else {}
		}
	}
	
//	private void printAll() {
//		for(int index = 0; index < this.results.size(); index ++) {
//			this.results.get(index).print();
//		}
//	}
	
	protected void takeDown() {
		System.out.println("UserAgent (" + getAID().getName()
				+ ") says goodbye.");
	}
	
}
